package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherMediaFailMapper;
import com.zjdiepu.www.ibiz.other.IOtherMediaFailService;
import com.zjdiepu.www.model.other.dto.OtherMediaFail;
import com.zjdiepu.www.model.other.ext.OtherMediaFailExt;

/**
 * 多媒体处理记录Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年06月26日 10时41分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherMediaFailServiceImpl extends BaseService implements IOtherMediaFailService {
	public static final Logger logger = LoggerFactory.getLogger(OtherMediaFailServiceImpl.class);

	@Autowired
	private OtherMediaFailMapper otherMediaFailMapper;
	
	@Override
	public void saveUpdateAndDelete(OtherMediaFail otherMediaFail, OperateType operType) {
		switch (operType) {
		case add:
			otherMediaFail.initInsertData(otherMediaFail.getModifier(), otherMediaFail.getModifierName());
			otherMediaFailMapper.insert(otherMediaFail);
			break;
		case edit:
			otherMediaFail.initUpdateData(otherMediaFail.getModifier(), otherMediaFail.getModifierName());
			// 影响的记录数
			int updateCount = otherMediaFailMapper.updateByPrimaryKeySelective(otherMediaFail);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherMediaFail.initDeleteData(otherMediaFail.getModifier(), otherMediaFail.getModifierName());
			// 影响的记录数
			int deleteCount = otherMediaFailMapper.updateByPrimaryKeySelective(otherMediaFail);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherMediaFail> selectByExt(OtherMediaFailExt otherMediaFailExt) {
		return otherMediaFailMapper.selectByExt(otherMediaFailExt);
	}
	
	@Override
	public int countByExt(OtherMediaFailExt otherMediaFailExt){
		return otherMediaFailMapper.countByExt(otherMediaFailExt);
	}
	
	@Override
	public OtherMediaFail selectById(String id) {
		return otherMediaFailMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherMediaFailMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherMediaFail otherMediaFail) {
		return otherMediaFailMapper.countRefined(otherMediaFail);
	}

	@Override
	public OtherMediaFail selectRefined(OtherMediaFail otherMediaFail) {
		return otherMediaFailMapper.selectRefined(otherMediaFail);
	}
	
	@Override
	public int deletedRefined(OtherMediaFail otherMediaFail) {
		return otherMediaFailMapper.deletedRefined(otherMediaFail);
	}

	@Override
	public boolean repeat(OtherMediaFail otherMediaFail) {
		int count = countRefined(otherMediaFail);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherMediaFail.getId())) {
			return true;
		}
		OtherMediaFail dbItem = selectRefined(otherMediaFail);
		if(null == dbItem) {
			return false;
		}
		return !otherMediaFail.getId().equals(dbItem.getId());
	}
	
	@Override
	public int insertBatch(List<OtherMediaFail> otherMediaFails) {
		return otherMediaFailMapper.insertBatch(otherMediaFails);
	}
}
